package com.leetcode.LC_MED;

public class LC134 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int costSum = 0, gasSum = 0;
        for (int i = 0; i < gas.length; i++) {
            costSum += cost[i];
            gasSum += gas[i];
        }
        if (costSum > gasSum) return -1;
        int currentGas = 0, start = 0;
        for (int i = 0; i < gas.length; i++) {
            currentGas = currentGas - cost[i] + gas[i];
            // 如果到达下一站的时候油量为负数，就以这个站点为起点重新计算
            if (currentGas < 0) {
                currentGas = 0;
                start = i + 1;
            }
        }
        return start;
    }
}
